﻿using Hotel_Project.Tools.MySqlData_Helps;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hotel_Project.BIZ
{
    internal class ScheduledRegistrationBIZ
    {
        //建立连接字符串(可将它写到配置文件中) server:IP  port：端口号 user：数据库名称 password：密码 database 表名 charset：编码格式
        public const string MYSQL_CONNAND = "server=192.168.0.56;user=xxxx;database=hotel;port=3306;password=123456";
        //查询st数据库(可将它写到配置文件中)
        public const string MYSQL_SELECT_st = "SELECT * FROM `bookingform`";

        /// <summary>
        /// 增加功能
        /// </summary>
        public void Add(string identityCard, int phoneNumber, string guestPhone, char guestSex, string scheduled_Time, string appointmentTime, string froor, int roomNumber)
        {
            string instruct = "insert into bookingform VALUES" +
             "(@identityCard1,@phoneNumber1,@guestPhone1,@guestSex1,@scheduled_Time1,@appointmentTime1,@froor1,@roomNumber1)";
            using (MySqlConnection conn = new MySqlConnection(MYSQL_CONNAND))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(instruct, conn);
                MySqlParameter parma = new MySqlParameter("identityCard1", identityCard);
                MySqlParameter parma2 = new MySqlParameter("phoneNumber1", phoneNumber);
                MySqlParameter parma3 = new MySqlParameter("guestPhone1", guestPhone);
                MySqlParameter parma4 = new MySqlParameter("guestSex1", guestSex);
                MySqlParameter parma5 = new MySqlParameter("scheduled_Time1", scheduled_Time);
                MySqlParameter parma6 = new MySqlParameter("appointmentTime1", appointmentTime);
                MySqlParameter parma7 = new MySqlParameter("froor1", froor);
                MySqlParameter parma8 = new MySqlParameter("roomNumber1", roomNumber);
                cmd.Parameters.AddRange(new MySqlParameter[] { parma, parma2, parma3, parma4, parma5, parma6, parma7, parma8 });
                MessageBox.Show("添加成功" + cmd.ExecuteNonQuery());
            }
        }
        /// <summary>
        /// 删除功能
        /// </summary>
        public void Delete()
        {

        }
        /// <summary>
        /// 修改功能
        /// </summary>
        public void Amend()
        {

        }
        /// <summary>
        /// 退出功能
        /// </summary>
        public void Quit()
        {

        }

        public  DataTable GetDataTable(string sqlCommand, string sql)
        {
            // 创建DataTable 表用来存储数据
            DataTable table = new DataTable();//单个数据表
            using (MySqlConnection conn = new MySqlConnection(sqlCommand))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                //可以将查询到的Mysql数据填充到table表单里面
                MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
                //将查询到的数据填充到table表格里面。
                dataAdapter.Fill(table);
            }
            return table;
        }
        public  List<T> Table2List<T>(DataTable table)
        {
            //通过反射生成 type对象进行反射操作
            Type type = typeof(T);
            //获取类的属性
            PropertyInfo[] p = type.GetProperties();

            List<T> list = new List<T>();
            //先判断你传入的类与你Sql语句查询的表的列数是不是相同
            if (p.Length != table.Columns.Count)
            {
                throw new Exception("类中属性与表中的列数不一致", new Exception());
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                T tt = Activator.CreateInstance<T>();
                for (int k = 0; k < table.Columns.Count; k++)
                {
                    if (table.Columns[k].ToString().ToLower() == p[k].Name.ToLower())
                    {
                        p[k].SetValue(tt, table.Rows[i][k]);
                    }
                }
                list.Add(tt);
            }
            return list;
        }
    }
}
